<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">注销</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width=" isCollapse?'64px':'200px' ">
        <div class="toggleCollapse" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区 -->
        <!-- unique-opened 默认为flase，加上之后默认只保持一个子菜单的展开，可以直接写上，也可以 通过v-bind绑定属性的布尔值为true -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" :collapse-transition="false"
         unique-opened :collapse="isCollapse" router  :default-active="activePath">
         <!-- router 是开启el-menu自带的路由功能 -->
          <!-- 一级子菜单 -->
          <el-submenu :index=" item.id+ '' " v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconslist[item.order -1]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index=" '/' + subItem.path  "
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="savaNavState('/' + subItem.path )"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data: function () {
    return {
      menulist: [],
      iconslist: [
        'iconfont icon-users',
        'iconfont icon-tijikongjian',
        'iconfont icon-shangpin',
        'iconfont icon-danju',
        'iconfont icon-baobiao'
      ],
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    // 在dom创建时期获取高亮的index
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.setItem('token', '')
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.err(res.meta.err)
      this.menulist = res.data
      // console.log(res.data)
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态(列表显示高亮)
    savaNavState (activePath) {
      // 把菜单的index值赋值给本地
      window.sessionStorage.setItem('activePath', activePath)
      // 点击之后重新赋值
      this.activePath = activePath
      // console.log(this.activePath)
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #333744;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: 0;
  }
  .toggleCollapse{
    background-color: #4a5064;
    text-align: center;
    letter-spacing: .2em;
    color: #fff;
    line-height: 24px;
    font-size: 10px;
    cursor: pointer;
  }
}
.iconfont{
  margin-right: 10px;
}
.el-main {
  background-color: #eaedf1;
}

</style>
